// JavaScript Document
$(document).ready(function (e) {
	movetitle();
	huifu();
	fa();
	hui();
	fanye();
	//setInterval(movetitle,5000);
});

window.onscroll=function(){
	var top = document.body.scrollTop||document.documentElement.scrollTop;
	if(top>400){
		$(".left").css({"position":"fixed","left":"10%","top":"0px","width":"16%"});
	}else{
		$(".left").css({"position":"static","width":"20%"});
	}
}

function movetitle(){
	$("#title1").animate({"left":"10px","top":"2px"},3000,function(){
		$("#title1").animate({"left":"0px","top":"0px"},3000);
	});
	$("#title2").animate({"left":"20px","top":"3px"},3000,function(){
		$("#title2").animate({"left":"0px","top":"0px"},3000,function(){
			movetitle();
		});
	});
}

function huifu(){
	if($(".j_hui").length=0)
		return null;
	else{
		$(".j_hui").unbind("click");
		$(".j_hui").on("click",function(){
			$(".j_hui").each(function(){
				$(this).parent("p").parent(".nei").children(".huihui").slideUp({
					duration:400,
					queue:false
				});
			});
			$(this).parent("p").parent(".nei").children(".huihui").slideDown({
				duration:400,
				queue:false
			});
		});
	}
}
function login(){
	var login = art.dialog({
		title: '登陆',
		lock: true,
		background:'#000',
		fixed: true,
		left: '40%',
		top:'40%',
		opacity:0.7,
	    content: document.getElementById('login'),
	    id: 'EF893L',
	    drag: false,
    	resize: false
	});
}
function check(x,y){
	if(x.length>20)
		return false;
	if(x.length=0)
		return false;
	if(y.length>20)
		return false;
	if(y.length=0)
		return false;
	return true;
}
function loginnow(){
	var name = $(".name1").val();
	var pwd = $(".pwd1").val();
	if(check(name,pwd)){
		$.post("/index/login",{"name":name,"pwd":pwd},function (reg){
			if(reg.status==0){
				$(".name1").val("");
				$(".pwd1").val("");
				alert(reg.mass);
			}else{
				$(".name1").val("");
				$(".pwd1").val("");
				alert(reg.mass);
				var list = art.dialog.list;
				for (var i in list) {
				    list[i].close();
				};
			}
		});
	}else{
		$(".name1").val("");
		$(".pwd1").val("");
		alert("格式有误");
	}
}
function newlogin(){
	var login = art.dialog({
		title: '注册',
		lock: true,
		background:'#000',
		fixed: true,
		left: '40%',
		top:'40%',
		opacity:0.7,
	    content: document.getElementById('login2'),
	    id: 'EF894L',
	    drag: false,
    	resize: false
	});
}
function newloginnow(){
	var name = $(".name2").val();
	var pwd = $(".pwd2").val();
	if(check(name,pwd)){
		$.post("/index/newlogin",{"name":name,"pwd":pwd},function (reg){
			if(reg.status==0){
				$(".name1").val("");
				$(".pwd1").val("");
				alert(reg.mass);
			}else{
				$(".name2").val("");
				$(".pwd2").val("");
				alert(reg.mass);
				var list = art.dialog.list;
				for (var i in list) {
				    list[i].close();
				};
			}
		});
	}else{
		$(".name2").val("");
		$(".pwd2").val("");
		alert("格式有误");
	}
}
function fa(){
	if($(".j_fa").length=0)
		return null;
	else{
		$(".j_fa").unbind("click");
		$(".j_fa").on("click",function(){
			if($(".j_ping1").val()==""){
				alert('评论不能为空');
			}else{
				$.post("/index/checklogin",null,function (reg){
					if(reg.status==0){
						alert('要登陆才行');
						login();
					}else{
						//alert($(".j_ping1").val());
						//console.log($(".pin").children(".nei:last").html());
						var user = reg.mass;
						var id = $(".newnei").attr("data");
						var date = new Date();
						var month = date.getMonth() + 1;
						var strDate = date.getDate();
						if (month >= 1 && month <= 9) {
							month = "0" + month;
						}
						if (strDate >= 0 && strDate <= 9) {
					        strDate = "0" + strDate;
					    }
					    var currentdate = date.getFullYear() + "-" + month + "-" + strDate;
					    $.post("/index/makeping",{"id":id,"user":user,"ping":$(".j_ping1").val(),"time":currentdate},function (ret){
					    	if(ret.status == 0){
					    		alert(ret.mass);
					    	}else{
					    		//console.log(ret.mass);
					    		//alert(ret.mass[0]["pid"]);
					    		$("#nei_1").html(user);
					    		$("#nei_2").html($(".j_ping1").val());
					    		$("#nei_3").html(currentdate+'<a class="fr j_hui" href="javascript:void(0)">回复</a><div class="clear"></div>');
					    		$(".j_ping1").val("");
					    		$(".newnei").children(".huihui").children("a").attr("data",ret.mass[0]["pid"]);
					    		$(".newnei").css('display',"block");
					    		$(".newnei").animate({"top":"-60px"},200,function(){
					    			$(".newnei").animate({"top":"-10px"},300,function(){
					    				$(".newnei").animate({"top":"-60px"},150,function(){
					    					$(".newnei").animate({"top":"-40px"},200,function(){
					    						$(".newnei").animate({"top":"-60px"},100,function(){
					    							$(".pin").children(".nei:last").after("<div class='nei' style='display:none'>"+$(".newnei").html()+"</div>");
										    		$(".newnei").css('display',"none");
													$(".newnei").css({"top":"30px",});
					    							$(".pin").children(".nei:last").slideDown(200);
					    							$(".pin").children(".nei:last").children(".nei_1").removeAttr("id");
									    			$(".pin").children(".nei:last").children(".nei_2").removeAttr("id");
									    			$(".pin").children(".nei:last").children(".nei_3").removeAttr("id");
					    							huifu();
					    							fa();
					    							hui();
									    			//$("#nei_1").html('');
										    		//$("#nei_2").html('');
										    		//$("#nei_3").html('');
					    						});
					    					});
					    				});
					    			});
					    		});
					    	}
					    });
					}
				});
			}
		});
	}
}
function hui(){
	if($(".j_huinow").length=0)
		return null;
	else{
		$(".j_huinow").unbind("click");
		$(".j_huinow").on("click",function(){
			if($(this).prev("textarea").val()==""){
				alert('评论不能为空');
			}else{
				var a = $(this);
				$.post("/index/checklogin",null,function (reg){
					if(reg.status==0){
						alert('要登陆才行');
						login();
					}else{
						//alert($(".j_ping1").val());
						//console.log(a.prev("textarea").val(),a.attr("data"));
						var user = reg.mass;
						var id = a.attr("data");
						var date = new Date();
						var month = date.getMonth() + 1;
						var strDate = date.getDate();
						if (month >= 1 && month <= 9) {
							month = "0" + month;
						}
						if (strDate >= 0 && strDate <= 9) {
					        strDate = "0" + strDate;
					    }
					    var currentdate = date.getFullYear() + "-" + month + "-" + strDate;
					    $.post("/index/makepingdec",{"id":id,"user":user,"ping":a.prev("textarea").val(),"time":currentdate},function (ret){
					    	if(ret.status == 0){
					    		alert(ret.mass);
					    	}else{
					    		$("#nei_1").html(user);
					    		$("#nei_2").html(a.prev("textarea").val());
					    		$("#nei_3").html(currentdate+'<a class="fr j_hui" href="javascript:void(0)">回复</a><div class="clear"></div>');
					    		a.prev("textarea").val("");
					    		a.parent(".huihui").before("<div class='nei' style='display:none'>"+$(".newnei").html()+"</div>");
					    		//console.log($(".newnei").html());
					    		a.parent(".huihui").parent(".nei").children(".nei_3").children("a").remove();
					    		a.parent(".huihui").slideUp(200,function(){
					    			a.parent(".huihui").parent(".nei").children(".nei").slideDown(200);
					    			var b = a.parent(".huihui").parent(".nei");
					    			a.parent(".huihui").remove();
					    			b.children(".nei").children(".huihui").children("a").attr("data",id);
					    			b.children(".nei").children(".nei_1").removeAttr("id");
					    			b.children(".nei").children(".nei_2").removeAttr("id");
					    			b.children(".nei").children(".nei_3").removeAttr("id");
					    			huifu();
					    			hui();
					    		});
					    	}
					    });
					}
				});
			}
		});
	}

}
function fanye(){
	$(".prevpage").on("click",function(){
		if($(this).attr("data")){
			location.href="/index/detail/id/"+$(this).attr("data");
		}
	});
	$(".nextpage").on("click",function(){
		if($(this).attr("data")){
			location.href="/index/detail/id/"+$(this).attr("data");
		}
	});
}